﻿Imports System.Windows.Controls.Canvas

Public Class clsLabel
    Inherits TextBox

    Private m_Ancestor As Object
    Private m_Editor As shpEditor
    Private m_PntClick As Point
    Private m_Left As Double, m_Top As Double

    Public ReadOnly Property Ancestor() As Object
        Get
            Ancestor = m_Ancestor
        End Get
    End Property

    Public ReadOnly Property ClickX() As Integer
        Get
            ClickX = m_PntClick.X
        End Get
    End Property

    Public ReadOnly Property ClickY() As Integer
        Get
            ClickY = m_PntClick.Y
        End Get
    End Property

    Public ReadOnly Property nLeft() As Double
        Get
            nLeft = m_Left
        End Get
    End Property

    Public ReadOnly Property nTop() As Double
        Get
            nTop = m_Top
        End Get
    End Property

    Public Sub New(ByVal ancestor As Object)
        m_Ancestor = ancestor
        m_Editor = GetEditor(ancestor)
        Background = Brushes.Transparent
        TextAlignment = Windows.TextAlignment.Center
        TextWrapping = Windows.TextWrapping.Wrap
        FontWeight = FontWeights.Bold
        Edit(False)
    End Sub

    Public Sub MoveShape(ByVal x As Integer, ByVal y As Integer)
        SetLeft(Me, x) : SetTop(Me, y)
        m_Left = x / m_Editor.ZoomX : m_Top = y / m_Editor.ZoomY
    End Sub

    Public Sub Edit(Optional ByVal flag As Boolean = True)
        If flag Then
            Focusable = True
            IsReadOnly = False
            Cursor = Nothing
            BorderThickness = New Thickness(1)
            Focus()
        Else
            Focusable = False
            IsReadOnly = True
            BorderThickness = New Thickness(0)
            Cursor = Cursors.Arrow
        End If
    End Sub

    Private Sub shpLabel_MouseLeftButtonDown(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
        m_PntClick.X = e.GetPosition(Me).X : m_PntClick.Y = e.GetPosition(Me).Y
        Focus()
    End Sub

    Private Sub shpLabel_MouseDoubleClick(ByVal sender As Object, ByVal e As MouseButtonEventArgs) Handles Me.MouseDoubleClick
        Edit()
    End Sub

    Private Sub shpLabel_LostFocus(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles Me.LostFocus
        Edit(False)
    End Sub

    Protected Sub shpLabel_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Me.KeyDown
        If e.Key = Input.Key.Enter Then
            Edit(False)
        ElseIf e.Key = Input.Key.F2 Then
            Edit()
        End If
    End Sub
End Class
